﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DbAdapter.sqldbAdapter;
using System.Data.SqlClient;
using System.Data;

namespace DAO.WaiGaoQiao
{
    public class DataAccessWork_Article
    {
        /// <summary>
        /// 根据id获取劳资物品对象
        /// </summary>
        /// <returns></returns>
        public DataSet GetWork_ArticleById(int Id)
        {
            sqldb db = new sqldb();
            string query = "select ID,ArticleNo,ArticleName,ArticleUnit,PriorID,GetType,Color from Work_Article where 1=1 and Id = " + Id;
            return db.ExecuteDataset(GetConntionString.conntionstring(), CommandType.Text, query);
        }


        /// <summary>
        /// 根据条件获取所有劳资物品。
        /// </summary>
        /// <returns></returns>
        public DataSet GetAllWork_Article(string condition)
        {
            sqldb db = new sqldb();
            string query = "select ID,ArticleNo,ArticleName,ArticleUnit,PriorID,GetType,Color from Work_Article where 1=1 " + condition;
            return db.ExecuteDataset(GetConntionString.conntionstring(), CommandType.Text, query);
        }

        /// <summary>
        /// 获取所有劳资物品(分页）
        /// </summary>
        /// <returns></returns>
        public DataSet GetAllWork_Article(string Condition, int pagesize, int startindex)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                int startrow = pagesize * (startindex - 1);
                int endrow = pagesize * startindex + 1;
                string query = "select B.ID,B.ArticleNo,B.ArticleName,B.ArticleUnit,B.PriorID,B.GetType,B.Color from (select ROW_NUMBER() OVER(ORDER BY A.ID DESC) rownumber,"
                             + "A.ID,A.ArticleNo,A.ArticleName,ArticleUnit,A.PriorID,A.GetType,Color from Work_Article A where 1=1 " + Condition + ") B "
                             + "where B.rownumber > " + startrow.ToString() + " and B.rownumber < " + endrow.ToString();
                return db.ExecuteDataset(conn, CommandType.Text, query);
            }
        }

        /// <summary>
        /// 保存或修改劳保物资
        /// </summary>
        /// <param name="dmpara"></param>
        /// <returns></returns>
        public int SaveWork_Article(object[] ArticleType)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                return db.ExecuteNonQuery(conn, "Proc_Work_Article", ArticleType);
            }
        }


        /// <summary>
        /// 删除劳保物资
        /// </summary>
        /// <param name="did"></param>
        /// <returns></returns>
        public int DeleteWork_Article(string id)
        {
            sqldb db = new sqldb();
            string query = "delete from Work_Article where  id = " + id;
            return db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
        }

        /// <summary>
        /// 根据条件获取所有劳资物品数量
        /// </summary>
        /// <returns></returns>
        public int GetWork_ArticleCount(string Condition)
        {
            sqldb db = new sqldb();
            string query = "select count(1) from Work_Article where 1=1 " + Condition;
            object rowcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return Convert.ToInt32(rowcount);
        }
       
        /// <summary>
        /// 根据ID获取劳保物品名称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string GetArticleNameByID(int id)
        {
            sqldb db = new sqldb();
            string query = "select isnull((select ArticleName from Work_Article where ID = " + id.ToString() + "),'') ArticleName";
            object gname = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return gname.ToString();
        }
      

        /// <summary>
        /// 是否存在子节点
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool IsExistSubNode(int id)
        {
            sqldb db = new sqldb();
            string query = "select count(1) from Work_Article where PriorID = " + id.ToString();
            object existcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return (Convert.ToInt32(existcount) > 0) ? true : false;
        }
    }
}
